package com.igeek.car.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.car.constant.MessageConstant;
import com.igeek.car.entity.Logs;
import com.igeek.car.result.Result;
import com.igeek.car.service.LogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.igeek.car.vo.PageQueryVO;
import org.springframework.web.bind.annotation.*;

/**
 * LogsController 日志接口，仅仅只做接口暴露
 * LogService    日志业务
 *      LogServiceImpl  日志业务的具体实现
 * LogMapper     日志的持久层交互
 *
 * Swagger测试地址：http://localhost:8080/swagger-ui.html
 *
 * @author Aries04r
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/admin/log")
@Api(tags = "日志的接口管理")
public class LogsController {

    @Autowired
    private LogsService logService;

    @ApiOperation("分页条件查询日志")
    @GetMapping("findPage")
    public Result findPage(PageQueryVO vo){
        Page<Logs> page = logService.findPage(vo);
        if(page != null){
            return new Result(true, MessageConstant.QUERY_LOGS_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_LOGS_FAIL);
    }

    @ApiOperation("新增日志")
    @PostMapping("add")
    public Result add(@RequestBody Logs log){
        boolean flag = logService.save(log);
        if(flag){
            return new Result(true, MessageConstant.ADD_LOGS_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_LOGS_FAIL);
    }

    @ApiOperation("通过ID查询日志信息")
    @GetMapping("{id}")
    public Result findOne(@PathVariable("id") Integer id){
        Logs log = logService.getById(id);
        if(log != null){
            return new Result(true, MessageConstant.QUERY_LOGS_SUCCESS, log);
        }
        return new Result(false, MessageConstant.QUERY_LOGS_FAIL);
    }

    @ApiOperation("编辑日志")
    @PutMapping("update")
    public Result update(@RequestBody Logs log){
        boolean flag = logService.updateById(log);
        if(flag){
            return new Result(true, MessageConstant.UPDATE_LOGS_SUCCESS);
        }
        return new Result(false, MessageConstant.UPDATE_LOGS_FAIL);
    }

    @ApiOperation("删除日志")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean flag = logService.del(id);
        if(flag){
            return new Result(true, MessageConstant.DELETE_LOGS_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_LOGS_FAIL);
    }
}
